package stuttgart.uni.bluetoothcontactexchange;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;

public class ContactData {

	ContentResolver cr;

	// Contentresolver wird von Service mit uebergeben
	public ContactData(ContentResolver contentResolver) {
		cr = contentResolver;
	}

	public PersonData getContact(Uri contactURI) {

		String personName;
		int contactId;

		String[] phoneNumbers = null;
		int[] phoneTypes = null;
		int phoneCount;

		String[] emailAdresses = null;
		int emailCount;

		PersonData result;

		Cursor c = cr.query(contactURI, null, null, null, null);
		c.moveToFirst();
		
		// Name wird ausgelesen
		personName = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

		contactId = c.getInt(c.getColumnIndex(ContactsContract.Contacts._ID));
		
		Cursor phones = cr.query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "+ contactId, null, null);
		
		Cursor emails = cr.query(
				ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
				ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "+ contactId, null, null);

		// Alle Telefonnummern werden ausgelesen
		phoneCount = phones.getCount();
		if (phoneCount > 0) {
			phoneNumbers = new String[phoneCount];
			phoneTypes = new int[phoneCount];
			if (phones.moveToFirst()) {
				int i = 0;
				do {
					phoneNumbers[i] = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					phoneTypes[i] = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
					i++;
				} while (phones.moveToNext());
			}

		}

		// email adressen werden ausgelesen
		emailCount = emails.getCount();
		if (emailCount > 0) {
			emailAdresses = new String[emailCount];
			if (emails.moveToFirst()) {
				int i = 0;
				do {
					emailAdresses[i] = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
					i++;
				} while (emails.moveToNext());
			}
		}

		result = new PersonData(personName, phoneNumbers, phoneTypes,
				emailAdresses);

		return result;
	}

	// Kontakt wird gespeichert
	public void saveContact(PersonData newPerson) throws RemoteException,
			OperationApplicationException {

		String personName = newPerson.getName();

		String[] phoneNumbers = newPerson.getNumbers();
		int[] phoneTypes = newPerson.getNumberTypes();
		int phoneCount;

		String[] emailAdresses = newPerson.getEmails();
		int emailCount;

		// Kontakt wird angelegt
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

		ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
				.withValue(RawContacts.ACCOUNT_TYPE, null)
				.withValue(RawContacts.ACCOUNT_NAME, null).build());
		ops.add(ContentProviderOperation
				.newInsert(Data.CONTENT_URI)
				.withValueBackReference(Data.RAW_CONTACT_ID, 0)
				.withValue(Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
				.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,personName)
				.build());

		//  Telefonnummern speichern
		if (phoneNumbers != null) {
			phoneCount = phoneNumbers.length;
			for (int i = 0; i < phoneCount; i++) {
				ops.add(ContentProviderOperation
						.newInsert(Data.CONTENT_URI)
						.withValueBackReference(Data.RAW_CONTACT_ID, 0)
						.withValue(Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
						.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,phoneNumbers[i])
						.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,phoneTypes[i])
						.build());
			}
		}

		//  Emails speichern
		if (emailAdresses != null) {
			emailCount = emailAdresses.length;
			for (int i = 0; i < emailCount; i++) {
				ops.add(ContentProviderOperation
						.newInsert(Data.CONTENT_URI)
						.withValueBackReference(Data.RAW_CONTACT_ID, 0)
						.withValue(Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
						.withValue(ContactsContract.CommonDataKinds.Email.DATA,emailAdresses[i])
						.build());
			}
		}

		cr.applyBatch(ContactsContract.AUTHORITY, ops);
	}
}
